package cn.jxufe.imp;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jxufe.bean.Message;
import cn.jxufe.dao.CropsGrowDAO;
import cn.jxufe.dao.FarmDAO;
import cn.jxufe.dao.FarmViewDAO;
import cn.jxufe.dao.SeedBagDAO;
import cn.jxufe.dao.SeedDAO;
import cn.jxufe.dao.UserDAO;
import cn.jxufe.entity.CropsGrow;
import cn.jxufe.entity.Farm;
import cn.jxufe.entity.FarmView;
import cn.jxufe.entity.Seed;
import cn.jxufe.entity.SeedBag;
import cn.jxufe.entity.User;
import cn.jxufe.service.FarmService;

@Service
public class FarmImp implements FarmService{
	@Autowired
	FarmDAO farmDAO;
	
	@Autowired
	FarmViewDAO farmViewDAO;
	
	@Autowired
	UserDAO userDAO;
	
	@Autowired
	SeedBagDAO seedBagDAO;
	
	@Autowired
	CropsGrowDAO cropsGrowDAO;
	
	@Autowired
	SeedDAO seedDAO;
	

	@Override
	public List<Farm> list() {
		return farmDAO.findAll();
	}

	@Override
	public void save(Farm farm) {
		farmDAO.save(farm);
	}
	
	// 种植
		@Override
		@Transactional
		public Message plant(int landId, int seedId, HttpSession session) {
			Message message = new Message();
			try {
				User curUser = (User) session.getAttribute("curUser");
				// 通过seedId 和 userId 获取seedBag，并将number - 1
				int userId = (int) curUser.getId();
				SeedBag seedBag = seedBagDAO.findByUserIdAndSeedId(userId, seedId);
				Farm farm = new Farm();
				int seedNumber = seedBag.getNumber();
				// 如果种子的数量为1，那么减1后直接删除该条记录
				if(seedNumber == 1) {
					seedBagDAO.delete(seedBag);
				}else {
					seedBag.setNumber(seedBag.getNumber() - 1);
					seedBagDAO.save(seedBag);
				}
				// 查询剩余的季度
				Seed seed = seedDAO.findBySeedId(seedId);
				int seasonCrops = seed.getxSeasonCrops();
				// 种子预收获
				int seasonHarvest = seed.getSeasonHarvest();
				
				farm.setUserId(userId);
				farm.setSeedId(seedId);
				farm.setLandId(landId);
				farm.setCurStage(0);
				farm.setCurStageThroughTime(0);
				farm.setHarvest(seasonHarvest);
				farm.setIsInsect(0);
				farm.setResidueSeason(seasonCrops - 1);
				// 保存种子
				farmDAO.save(farm);
				message.setCode(0); 
				message.setMsg("种植成功！");
			} catch (Exception e) {
				message.setCode(-10);
				message.setMsg("种植失败！");
				e.printStackTrace();
			}
			return message;
		}
	

	// 除虫
	@Override
	@Transactional
	public Message killInsect(int landId, HttpSession session) {
		Message message = new Message();
		try {
			User curUser = (User)session.getAttribute("curUser");
			
			Farm farm = farmDAO.findByLandIdAndUserId(landId, (int)curUser.getId());
			// 设置isInsect字段为2，使其不再生虫
			farm.setIsInsect(2);
			farmDAO.save(farm);
			
			curUser.setMoney(curUser.getMoney() + 1);
			curUser.setExperience(curUser.getExperience() + 2);
			curUser.setPoint(curUser.getPoint() + 2);
			// 保存数据
			userDAO.save(curUser);
			session.setAttribute("curUser", curUser);
			message.setCode(1);
			message.setMsg("除虫成功 <br> 经验：+2 <br> 金币：+1 <br> 积分：+2");
		} catch (Exception e) {
			message.setCode(-10);
			message.setMsg("除虫失败");
			e.printStackTrace();
		}

		return message;
	}

	// 收获
	@Override
	@Transactional
	public Message harvest(int landId, HttpSession session) {
		Message message = new Message();
		try {
			User curUser = (User)session.getAttribute("curUser");
			FarmView farmView = farmViewDAO.findByLandIdAndUserId(landId,(int)curUser.getId());
			Farm farm = farmDAO.findByLandIdAndUserId(landId,(int)curUser.getId());
			
			int experience = farmView.getExperience();
			int money = farmView.getHarvest() * farmView.getFruitUnitPrice();
			int point = farmView.getSeasonGetPoint();
			String seedName = farmView.getSeedName();
			
			curUser.setMoney(curUser.getMoney() + money);
			curUser.setExperience(curUser.getExperience() + experience);
			curUser.setPoint(curUser.getPoint() + point);
			
			// 收获过后，转为枯草期
			farm.setCurStage(9);
			farmDAO.save(farm);
			
			// 用户信息保存至数据库和session
			userDAO.save(curUser);
			session.setAttribute("curUser", curUser);
			message.setCode(0);
			message.setMsg(seedName + "收获成功<br>经验：+" + experience + "<br>金币：+" + money + "<br>积分：+" + point);
			
		} catch (Exception e) {
			message.setCode(-10);
			message.setMsg("收获失败");
			e.printStackTrace();
		}
		
		return message;
	}

	// 除草
	@Override
	@Transactional
	public Message cleanLand(int landId, HttpSession session) {
		Message message = new Message();
		try {
			User curUser = (User) session.getAttribute("curUser");
			FarmView farmView = farmViewDAO.findByLandIdAndUserId(landId, (int)curUser.getId());
			
			curUser.setExperience(curUser.getExperience() + 5);
			curUser.setPoint(curUser.getPoint() + 5);
			
			Farm farm = farmDAO.findByLandIdAndUserId(landId, (int)curUser.getId());
			if(farmView.getResidueSeason() == 0) {
				// 除完草，删除farm数据库里面的记录
//				farmDAO.delete(farmView.toFarm());
				farmDAO.delete(farm);
			}else {
				// 重新来过(季节减一，虫置位0，curStage=0，harvest设置为seed的初始值，curStageThroughTime = 0)
				farm.setResidueSeason(farm.getResidueSeason() - 1);
				farm.setIsInsect(0);
				farm.setCurStage(0);
				Seed seed = seedDAO.findBySeedId(farm.getSeedId());
				farm.setHarvest(seed.getSeasonHarvest());
				farm.setCurStageThroughTime(0);
				farmDAO.save(farm);  // 更新操作
			}
			
			// 用户信息保存至数据库和session
			userDAO.save(curUser);
			session.setAttribute("curUser", curUser);
			message.setCode(0);
			message.setMsg("除枯叶收获<br>经验：5<br>积分：+5");
		}catch (Exception e) {
			message.setCode(-10);
			message.setMsg("除草失败");
		}
		return message;
	}

	@Override
	@Transactional
	public Message addHarvest(int landId, HttpSession session) {
		Message message = new Message();
		try {
			User curUser = (User) session.getAttribute("curUser");
			Farm farm = farmDAO.findByLandIdAndUserId(landId, (int)curUser.getId());
			int upNum = 1;
			if(Math.random() > 0.5) upNum = 2;
			farm.setHarvest(farm.getHarvest() + upNum);
			farmDAO.save(farm);
			message.setCode(0);
			message.setMsg(landId + "_" + upNum);
		}catch (Exception e) {
			e.printStackTrace();
			message.setCode(-1);
			message.setMsg("系统错误！");
		}
		return message;
	}

}
